package com.fz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fz.entity.Notice;
import com.fz.service.NoticeService;
import com.fz.service.PageViewService;
import com.fz.vo.ResultVo;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 存储社区公告发布与状态 前端控制器
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    NoticeService tNoticeService;
    PageViewService tPageviewService;
    @GetMapping("/list")
    List<Notice> list(){
        List<Notice> tNotice=tNoticeService.list();

        return tNoticeService.updateview(tNotice);
    }
    @PostMapping("/list2")
    ResultVo<IPage<Notice>> list2(Integer pageNo, Integer pageSize){
        List<Notice> tNotice=tNoticeService.list();
        tNoticeService.updateview(tNotice);
        return ResultVo.success(tNoticeService.showList( pageNo, pageSize));
    }

    @PostMapping("/llist")
    ResultVo<IPage<Notice>>llist( Integer pageNo, Integer pageSize)
    {

        List<Notice> tNotice=tNoticeService.list();
        tNoticeService.updateview(tNotice);
        return ResultVo.success(tNoticeService.getList( pageNo, pageSize));}

    @PostMapping("/search")
    ResultVo<IPage<Notice>> search(String input,Integer pageNo, Integer pageSize){
        return ResultVo.success(tNoticeService.search(input,pageNo,pageSize),"搜索成功！");
    }

    @PostMapping("/add")
    ResultVo<Boolean> add(String title, String content, LocalDateTime time) {
        return ResultVo.success(tNoticeService.save(new Notice(title,content,time)), "添加公告成功");
    }
    @GetMapping("/getById")
    ResultVo<Notice> getById(Long id){
        return ResultVo.success(tNoticeService.getById(id));
    }
    @PostMapping("/del")
    ResultVo<Boolean> del(Long id,Integer statue,Integer statu){
        return ResultVo.success(tNoticeService.del(id,statue,statu),statu==0?"结束公告成功":"启用公告成功");
    }

    @PostMapping("/update")
    ResultVo<Boolean> update(Long id,String title,String content) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVo.success(tNoticeService.update(id,title,content),"修改公告成功");
    }
    //@PostMapping("/search")
    //ResultVo<List<Notice>> search(String input){
    //    return ResultVo.success(tNoticeService.search(input),"搜索成功");
    //}

    @GetMapping("/detail")
    String detail(Long id){
        String xyuyu="";
        if(null==id)
        {
            xyuyu="错误，联系管理员";
        }
        else {
            xyuyu= tNoticeService.getById(id).getContent();
        }
        return xyuyu;
    }
}
